//
//  LogOnViewController.m
//  uMeng
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-6-4.
//  Copyright 2011年 Mtime. All rights reserved.
//

#import "RootViewController.h"
#import "CONSTS.h"
#import "ButtonAndLabelView.h"
#import "LogOnViewController.h"
#import "QuartzCore/QuartzCore.h"
#import "uMengAppDelegate.h"
#import "NavigationController.h"

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
    [_logOnViewController release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)logOn
{
    uMengAppDelegate *delegate = (uMengAppDelegate*)[UIApplication sharedApplication].delegate;
    delegate.nav.view.userInteractionEnabled = NO;
    //[delegate showGlobalIndicator: YES];
    [delegate.window addSubview: _logOnViewController.view];
    
    CAKeyframeAnimation * animation; 
    
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 
    
    animation.duration = 0.5; 
    
    animation.delegate = self;
    
    animation.removedOnCompletion = YES;
    
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]]; 
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]]; 
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]]; 
    
    animation.values = values;
    
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    
    [_logOnViewController.view.layer addAnimation:animation forKey:nil];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.view setNeedsLayout];
    self.wantsFullScreenLayout = YES;
    
    
    self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"main_bg.png"]];// [UIColor colorWithRed:0.00 green:0.40 blue:0.80 alpha:1.00];
    
    self.navigationItem.title = @"uMeng统计";
    
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"登入" 
                                                                               style: UIBarButtonSystemItemPageCurl 
                                                                              target: self 
                                                                              action: @selector(logOn)] autorelease];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"App" 
                                                                              style: UIBarButtonSystemItemPageCurl 
                                                                             target: self 
                                                                             action: nil] autorelease];
    
    NSArray* imageNames = [NSArray arrayWithObjects: 
                           @"statistics.png",
                           @"Analysis.png", 
                           @"mobile_network.png", 
                           @"custom.png", 
                           @"connect.png", 
                           @"seting.png", nil];
    
    NSArray* pressImageNames = [NSArray arrayWithObjects: 
                                @"ico_mobile.png",
                                @"ico_idcard.png",
                                @"ico_postcode.png",
                                @"ico_flight.png", 
                                @"ico_translate.png", 
                                @"ico_phone.png", 
                                @"ico_car.png", 
                                @"ico_health.png", 
                                @"ico_bjxm.png", nil];
    
    NSArray *nameOfLabelArray = [NSArray arrayWithObjects: @"统计概况", @"用户分析", @"终端及网络", @"自定义事件",@"联系我们", @"设置", nil];
    
    ButtonAndLabelView *BtnL;
    
    for (int i=0; i<6; i++) {
        
        CGRect frame;
        float space = (320.0 - btnWidth * 3) / 4;
        float spaceOfLine = 18;
        
        UIImage *image = [UIImage imageNamed:[imageNames objectAtIndex: i]];
        UIImage *pressImage = [UIImage imageNamed:[pressImageNames objectAtIndex: i]];
        NSString *textOflabel = [nameOfLabelArray objectAtIndex: i];
        
        BtnL = [[[ButtonAndLabelView alloc] initWithButtonImage: image
                                               buttonPressImage: pressImage 
                                                       delegate: self 
                                                    textOfLabel: textOflabel 
                                                    tagOfbutton: (800 + i)] autorelease];
        
        frame.size.width = btnWidth;//设置按钮坐标及大小
        frame.size.height = btnHeight;
        frame.origin.x = (i % 3) * (btnWidth + space) + space;
        frame.origin.y = floor(i / 3) * (btnHeight + spaceOfLine) + 100 + 70 + 15 - 5 + 3 - 5;
        [BtnL setFrame:frame];
        
        [self.view addSubview:BtnL];
    }
    
    //初始化登录视图
    _logOnViewController = [[LogOnViewController alloc] init];
    _logOnViewController.view.frame = CGRectMake(0, 0, 289, 158);
    CGPoint center = CGPointMake(self.view.center.x, self.view.center.y - 20);
    _logOnViewController.view.center = center;
    
    NSString *lSystemVersion = [[UIDevice currentDevice] systemVersion];
    float sv = [lSystemVersion floatValue];
    if (sv > 4.0) //4.0版本以前的系统不支持阴影
    { 
        CALayer *sublayer = _logOnViewController.view.layer;
        sublayer.shadowOffset = CGSizeMake(0, 20);
        sublayer.shadowRadius = 5.0;
        sublayer.shadowColor = [UIColor blackColor].CGColor;
        sublayer.shadowOpacity = 0.8;
    }
}

- (void)btnPressed:(id)sender
{
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
